package cn.kim.edu.info.manager.controller;

import cn.kim.edu.info.manager.domain.Teacher;
import cn.kim.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {

    private TeacherService teacherService = new TeacherService();
    //键盘录入
    private Scanner sc = new Scanner(System.in);

    public void start() {

        teacherLoop:
        while (true) {
            System.out.println("------------欢迎使用<老师>管理系统----------------");
            System.out.println("请输入你的选择：1.添加老师 2.删除老师 3.修改老师 4.查看老师 5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacherById();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case "5":
                    System.out.println("感谢使用Kim老师管理系统！");
                    break teacherLoop;
                default:
                    System.out.println("你的输入有误！请重新输入");
            }
        }
    }

    //修改信息
    public void updateTeacher() {
        String id;
        while (true) {
            //键盘输入
            System.out.println("请输入你要修改的id：");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (!exists) {
                System.out.println("你输入的ID不存在，请重新输入");
            } else {
                break;
            }
        }

        Teacher newTeacher = inputTeacherInfo(id);

        //调用逻辑层的修改逻辑
        teacherService.updateTeacher(id, newTeacher);
        System.out.println("修改成功！");
    }

    //删除老师信息
    public void deleteTeacherById() {
        String id = inputTeacherId();
        //调用逻辑层的删除方法
        teacherService.deleteTeacherById(id);
        //提示删除成功
        System.out.println("删除成功！");

    }

    //查看老师信息
    public void findAllTeacher() {
        //逻辑层获取老师对象数组
        Teacher[] teachers = teacherService.findAllTeacher();

        //判断数组这是否有元素
        if (teachers == null) {
            System.out.println("查无信息，请先添加！");
            return;
        }
        //遍历数组并打印在控制台
        System.out.println("教师ID\t\t姓名\t\t年龄\t\t生日");
        for (int i = 0; i < teachers.length; i++) {
            Teacher t = teachers[i];
            if (t != null) {
                System.out.println(t.getId() + "\t" + t.getName() + "\t" + t.getAge() + "\t" + t.getBirthday());
            }
        }
    }

    //添加老师
    public void addTeacher() {
        String id = inputTeacherId();
        Teacher teacher = inputTeacherInfo(id);

        //5将封装好的的老师对象传递给TeacherService继续完成添加操作
        boolean result = teacherService.addTeacher(teacher);
        if (result) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }

    }

    //录入老师id
    public String inputTeacherId() {
        String id;
        while (true) {
            System.out.println("请输入ID：");
            id = sc.next();
            boolean exists = teacherService.isExists(id);
            if (exists) {
                System.out.println("id已存在！请重新录入！");
            } else {
                break;
            }
        }
        return id;
    }

    //录入老师信息封装
    public Teacher inputTeacherInfo(String id) {
        //3接收老师其他信息
        System.out.println("请输入老师姓名：");
        String name = sc.next();
        System.out.println("请输入老师年龄：");
        String age = sc.next();
        System.out.println("请输入老师生日：");
        String birthday = sc.next();

        //4封装为老师对象
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }


}
